#include<iostream>
#include"dateTime.h"

using namespace std;

bool isleapyear(int year){
    if((year%4 == 0) || !(year%100 == 0 && year%400 != 0))
    {
        return true;
    }
    return false;
}

int yearall(int dates,int& dateleft){
    int years = 1970;
    int i = 0;
    int dateall = 0;
    //int dateleft = 0;
    while(dateall < dates)
    {
        if(isleapyear(years + i))
        {
            dateall += 366;
        }else
        {
            dateall += 365;
        }
        i++;
    }
    dateleft = dates - dateall;
    return years + i;
}

int monthall(int& datesleft, bool isleapyear){
    int month = 0;
    if(isleapyear){
        if(datesleft <= 31)
        {
            month = 1;
        }else if(datesleft <= (31 + 29))
        {
            month = 2;
            datesleft -= 31;
        }else if(datesleft <= (31 + 29 + 31))
        {
            month = 3;
            datesleft -= 31 + 29;
        }else if(datesleft <= (31 + 29 + 31 + 30))
        {
            month = 4;
            datesleft -= 31 + 29 + 31;
        }else if(datesleft <= (31 + 29 + 31 + 30 + 31))
        {
            month = 5;
            datesleft -= 31 + 29 + 31 + 30;
        }else if(datesleft <= (31 + 29 + 31 + 30 + 31 + 30))
        {
            month = 6;
            datesleft -= 31 + 29 + 31 + 30 + 31;
        }else if(datesleft <= (31 + 29 + 31 + 30 + 31 + 30 + 31))
        {
            month = 7;
            datesleft -= 31 + 29 + 31 + 30 + 31 + 30;
        }else if(datesleft <= (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31))
        {
            month = 8;
            datesleft -= 31 + 29 + 31 + 30 + 31 + 30 + 31;
        }else if(datesleft <= (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30))
        {
            month = 9;
            datesleft -= 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31;
        }else if(datesleft <= (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31))
        {
            month = 10;
            datesleft -= 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30;
        }else if(datesleft <= (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30))
        {
            month = 11;
            datesleft -= 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;
        }else if(datesleft <= (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31))
        {
            datesleft -= 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30;
            month = 12;
        }
    }else{
        if(datesleft <= 31)
        {
            month = 1;
        }else if(datesleft <= (31 + 28))
        {
            month = 2;
            datesleft -= 31;
        }else if(datesleft <= (31 + 28 + 31))
        {
            month = 3;
            datesleft -= 31 + 28;
        }else if(datesleft <= (31 + 28 + 31 + 30))
        {
            month = 4;
            datesleft -= 31 + 28 + 31;
        }else if(datesleft <= (31 + 28 + 31 + 30 + 31))
        {
            month = 5;
            datesleft -= 31 + 28 + 31 + 30;
        }else if(datesleft <= (31 + 28 + 31 + 30 + 31 + 30))
        {
            month = 6;
            datesleft -= 31 + 28 + 31 + 30 + 31;
        }else if(datesleft <= (31 + 28 + 31 + 30 + 31 + 30 + 31))
        {
            month = 7;
            datesleft -= 31 + 28 + 31 + 30 + 31 + 30;
        }else if(datesleft <= (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31))
        {
            month = 8;
            datesleft -= 31 + 28 + 31 + 30 + 31 + 30 + 31;
        }else if(datesleft <= (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30))
        {
            month = 9;
            datesleft -= 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31;
        }else if(datesleft <= (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31))
        {
            month = 10;
            datesleft -= 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30;
        }else if(datesleft <= (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30))
        {
            month = 11;
            datesleft -= 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;
        }else if(datesleft <= (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31))
        {
            datesleft -= 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30;
            month = 12;
        }
    }
    datesleft += 1;
    return month;
}

int hourall(long& secondsleft){
    int hours = secondsleft / (60 * 60);
    secondsleft = secondsleft % (60 * 60);
    return hours;
}

int minuteall(long& secondsleft){
    int minutes = secondsleft / 60;
    secondsleft = secondsleft % 60;
    return minutes;
}

int datestodate(long dates){
    int datelefts = 0;
    int years = yearall(dates, datelefts);
    int month = monthall(datelefts, isleapyear(years));
    //int dates = datelefts;
    std::cout << "years is " << years << " months is " << month << " dates is " << datelefts << std::endl;
    return 0;
}

int secondToDateTime(long seconds){
    int dates = seconds / (60 * 60 * 24);
    long secs = seconds % (60 * 60 * 24);
    datestodate(dates);
    int hours = hourall(secs);
    int minutes = minuteall(secs);
    std::cout << "hours is " << hours << " minutes is " << minutes << " secs is " << secs << std::endl; 
    return 0;
}

int main() {
    long long seconds = 0;
    while (1)
    {
        std::cout << "请输入秒数： " << std::endl;
        std::cin >> seconds;
        secondToDateTime(seconds);
    }
    return 0;
}